<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

class pqcesModelNuevoac extends JModel
{
	function __construct()
	{
		parent::__construct();
		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
	}

	function setId($id)
	{
		$this->_id	= $id;
		$this->_data	= null;
		$this->_datac	= null;
	}

	function &getData()
	{
		if (empty( $this->_data )) {
			$query = ' SELECT * FROM #__qcalendar'.
					'  WHERE id = '.$this->_id;
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();
		}
		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->id = 0;
			$this->_data->type = 0;
			$this->_data->refdate = '0000-00-00';
			$this->_data->created='0000-00-00';
			$this->_data->enddate='0000-00-00';
			$this->_data->link = 'div';
			$this->_data->url = null;
			$this->_data->hr = null;
			$this->_data->min = null;
			$this->_data->thr = 0;
			$this->_data->tmin = 0;
			$this->_data->category_id = null;
			$this->_data->email_alert = null;
			$this->_data->sms_alert = null;
			$this->_data->cron_email = null;
			$this->_data->cron_sms_number = null;
			$this->_data->short_desc = null;
			$this->_data->long_desc = null;
			$this->_data->short_desc_image = null;
			$this->_data->long_desc_image = null;
			$this->_data->active = null;
		}
		return $this->_data;
	}
	function &getCat(){
		if (empty( $this->_datac )) {
			$query = ' SELECT * FROM #__qcalendar_category';//. " WHERE active ='1'"; Si la categoría está desactiva, sólo se mostrará un mensaje genérico o nada
			$this->_db->setQuery( $query );
			$this->_datac = $this->_db->loadObjectList();
		}
		if (!$this->_datac) {
			$tmpC = new stdClass();
			$tmpC->id=0;
			$tmpC->short_desc=null;
			$this->_datac= array($tmpC);
		}
		return $this->_datac;
	}

	function store()
	{	
		$row =& $this->getTable();
		$data = JRequest::get( 'post' );
		$f=explode('-',$data['refdate']);
		$data['refdate']=$f[2].'-'.$f[1].'-'.$f[0];
		$f=explode('-',$data['enddate']);
		$data['enddate']=$f[2].'-'.$f[1].'-'.$f[0];
		unset($f);
		$data['long_desc']=JRequest::getVar( 'long_desc', '', 'post', 'string', JREQUEST_ALLOWHTML );
		unset($data['created']);
		($data['type'])? '': $data['enddate']='0000-00-00';
		$tmp = &$this->getData();
		if ($tmp->created=='0000-00-00'){
			$data['created']= date('Y-m-d');
		}
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		if (!$row->check()) {
			$this->setError($row->getError());
			return false;
		}
		if (!$row->store()) {
			$this->setError( $row->getError() );
			return false;
		}

		return $row->id;
	}
	function toggle(&$data){
		$row =& $this->getTable();
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		if (!$row->store()) {
			$this->setError( $row->getError() );
			return false;
		}
	}
	function delete()
	{
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );

		$row =& $this->getTable();

		if (count( $cids )) {
			foreach($cids as $cid) {
				if (!$row->delete( $cid )) {
					$this->setError( $row->getError() );
					return false;
				}
			}
		}
		return true;
	}
}
